﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Sinacor.Infra.Common.ErrorLogging;
using PSE.Framework.ErrorLogging;
using PSE.ProcessManager.Server.Process.Security;
using Sinacor.Infra.Core.ProcessManager.Server.Security;

namespace Sinacor.Infra.Core.ProcessManager.Server.ErrorLogging
{
    public class ProcessEnvironmentCollector : SinacorEnvironmentCollector
    {
        protected override PSE.Framework.ErrorLogging.EnvironmentData CreateEnvironmentData()
        {
            return new ProcessEnvironmentData();
        }

        protected override string GetVersion()
        {
            return "-";
        }

        public override PSE.Framework.ErrorLogging.EnvironmentData Collect(Dictionary<string, object> extensionData)
        {
            EnvironmentData environmentData = base.Collect(extensionData) as EnvironmentData;

            if (environmentData != null &&
                environmentData is ProcessEnvironmentData)
            {
                ProcessEnvironmentData processEnvironmentData = (ProcessEnvironmentData)environmentData;

                try
                {
                    if (AuthenticationContext<SinacorAuthenticationData>.Current != null &&
                        AuthenticationContext<SinacorAuthenticationData>.Current.AuthenticationData != null)
                    {
                        processEnvironmentData.SinacorUserName = this.GetData<string>(() => AuthenticationContext<SinacorAuthenticationData>.Current.AuthenticationData.LoginName, "SinacorUserName", environmentData);
                        processEnvironmentData.SinacorCompany = this.GetData<string>(() => AuthenticationContext<SinacorAuthenticationData>.Current.AuthenticationData.CompanyId + " - " + AuthenticationContext<SinacorAuthenticationData>.Current.AuthenticationData.CompanyName, "SinacorCompany", environmentData);
                        processEnvironmentData.SinacorModule = this.GetData<string>(() => AuthenticationContext<SinacorAuthenticationData>.Current.AuthenticationData.ModuleId, "SinacorModule", environmentData);
                    }

                    if (extensionData != null)
                    {
                        if (extensionData.ContainsKey("ProcessId"))
                            processEnvironmentData.ProcessId = this.GetData<int>(() => (int)extensionData["ProcessId"], "ProcessId", environmentData);

                        if (extensionData.ContainsKey("ProcessName"))
                            processEnvironmentData.ProcessName = this.GetData<string>(() => (string)extensionData["ProcessName"], "ProcessName", environmentData);
                    }
                }
                catch (Exception ex)
                {
                    processEnvironmentData = (ProcessEnvironmentData)HandleException(processEnvironmentData, ex);
                }
            }

            return environmentData;
        }
    }
}
